const express = require('express')
const db = require('../config/db')
const md5 = require('md5')
const config = require('../config')
const jwt = require('jsonwebtoken')

const router = express.Router()

//  登录
router.get('/user', async (req, res) => {
    const { username, password } = req.body
    const [ rows ] = await db.query('select * from user where username = ?', username)
    if (rows.length === 0) return res.send('用户名或密码错误！')
    if (rows[0].password !== md5(password)) return res.send('用户名或密码错误！')
    const user = { ...rows[0], password: '', avatar: '' }
    res.send({
        status: 0,
        message: '登录成功',
    })
})

//  注册
router.post('/login', async (req, res,next) => {
    const { username, password } = req.body
    const [ users ] = await db.query('select * from user where username = ?', username)
    if (users.length) return res.send('用户名已存在！！！')
        await db.query('insert into user (username, password) values (?, ?)', [username, md5(password)])
        res.send({
            status: 0,
            message: '注册成功',
        })
})


module.exports = router